package se.webbzon.boltzmann.object.geometry;

import java.nio.ByteBuffer;

import se.webbzon.oschi01.worldobject.WorldObject;

public class MaskedGeometryDeflation extends UnmaskedGeometryDeflation {
	
	private int maskGeometry;
	private boolean maskOpen;
	private int maskDivisions;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public MaskedGeometryDeflation(int deflationProperties) {
		this(deflationProperties,(WorldObject[]) null);
	}
	
	public MaskedGeometryDeflation(	int deflationProperties,
									WorldObject[] dependencies) {
		super(deflationProperties,dependencies);
	}
	
	public MaskedGeometryDeflation(int deflationProperties, MaskedGeometry g) {
		this(deflationProperties,null,g);
	}
	
	public MaskedGeometryDeflation(	int deflationProperties, 
									WorldObject[] dependencies, 
									MaskedGeometry g) {
		super(deflationProperties,dependencies,g);
		
		// Store the mask and solidity
		final GeometryMask mask = g.getGeometryMask();
		if (mask != null) {
			maskGeometry = mask.getGeometry().ordinal();
			maskOpen = mask.isOpen();
			maskDivisions = mask.getDivisions();
		} else {
			maskGeometry = GeometryEnum.XYPLANE.ordinal();
			maskOpen = false;
			maskDivisions = 0;
		}
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Creates a new geometry mask from the data stored in the 
	 * masked geometry deflation. **/
	public GeometryMask makeGeometryMask() {
		return new GeometryMask(GeometryEnum.values()[maskGeometry],
								maskOpen,maskDivisions);
	}

	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		maskGeometry = buff.getInt();
		maskOpen = buff.get()!=0;
		maskDivisions = buff.getInt(); 
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 9);
		
		buff.putInt(maskGeometry);
		buff.put(maskOpen ? (byte) 1 : (byte) 0);
		buff.putInt(maskDivisions);
		
		return buff;
	}
	
}
